package com.mc.cloud.mall.categoryProduct.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.cloud.mall.categoryProduct.model.mapper.ProductMapper;
import com.mc.cloud.mall.categoryProduct.model.pojo.Product;
import com.mc.cloud.mall.categoryProduct.model.query.ProductListQuery;
import com.mc.cloud.mall.categoryProduct.model.vo.CategoryVO;
import com.mc.cloud.mall.categoryProduct.model.vo.ProductList;
import com.mc.cloud.mall.common.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserProductServiceImpl
 * @Author MC
 * @Date 2020/11/5 17:41
 * @Version 1.0
 **/
@Service
public class UserProductServiceImpl implements UserProductService {
    
    @Autowired
    ProductMapper productMapper;
    
    @Autowired
    CategoryService categoryService;
    
    @Override
    public Product productDetail(Integer id){
        
        Product product = productMapper.selectByPrimaryKey(id);
        return product;
    
    }
    
    @Override
    public PageInfo list(ProductList productList){
        //构建查询对象
        ProductListQuery productListQuery = new ProductListQuery();
        
        //搜索处理
        if (!StringUtils.isEmpty(productList.getKeyword())){
            
            String keyword = new StringBuilder().append("%").append(productList.getKeyword()).append("%").toString();
            productListQuery.setKeyword(keyword);
        }
        
        //处理目录
        if (productList.getCategoryId()!=null){
            
            List<CategoryVO> categoryVOS = categoryService.listForCustom(productList.getCategoryId());
            ArrayList<Integer> categoryIds = new ArrayList<>();
            categoryIds.add(productList.getCategoryId());
            getCategoryIds(categoryVOS,categoryIds);
            productListQuery.setCategoryIds(categoryIds);
        }
        
        //排序处理
        String orderBy = productList.getOrderBy();
        if (Constant.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
            PageHelper.startPage(productList.getPageNum(),productList.getPageSize(),orderBy);
        }else{
            PageHelper.startPage(productList.getPageNum(),productList.getPageSize());
        }
    
        List<Product> products = productMapper.selectList(productListQuery);
        PageInfo pageInfo = new PageInfo(products);
        return pageInfo;
    }
    
    private void getCategoryIds(List<CategoryVO> categoryVOS,ArrayList<Integer> categoryIds){
        for (CategoryVO categoryVO : categoryVOS) {
            if (categoryVO.getChildCategory()!=null){
                categoryIds.add(categoryVO.getId());
                getCategoryIds(categoryVO.getChildCategory(),categoryIds);
            }
        }
    }
    
    @Override
    public void updateProductForFeign(Integer productId,Integer stock){
    
        Product product = new Product();
        product.setId(productId);
        product.setStock(stock);
        productMapper.updateByPrimaryKeySelective(product);
        
    }
}
